package com.hxd.otc.other.rong;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.module.order.activity.BuyOrderDetailActivity;
import com.hxd.otc.module.order.activity.OrderFinishedActivity;
import com.hxd.otc.module.order.activity.SaleOrderDetailActivity;
import com.hxd.otc.module.order.bean.AllOrderBean;
import com.hxd.otc.module.order.fragment.AllOrderFragment;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.LogUtils;
import com.hxd.otc.utils.StringUtil;

import io.rong.imkit.RongIM;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

/**
 * Created by lzy on 2018/8/10 0010.
 * description ${订单信息条目提供者}
 */
@ProviderTag(messageContent = OrderInfoFloatingMessage.class, showPortrait = false, centerInHorizontal = true, showSummaryWithName = false, showReadState = false)
public class OrderInfoFloatingMessageItemProvider extends IContainerItemProvider.MessageProvider<OrderInfoFloatingMessage> {
    @Override
    public void bindView(final View view, int i, final OrderInfoFloatingMessage content, final UIMessage message) {
        final ViewHolder holder = (ViewHolder) view.getTag();
        AllOrderBean.DataBean orderBean = content.getOrderBean();
        if (orderBean == null) return;
        holder.tvTransactionAmount.setText(String.format("%s%s", StringUtil.format8Number(orderBean.getTotalPrice()), orderBean.getSupportCurrencyCode()));
        holder.tvOrderId.setText(String.format("#%s", orderBean.getSubOrderId()));
        holder.tvNums.setText(String.format("%s%s", StringUtil.format8Number(orderBean.getVolume()), orderBean.getSymbol()));
        final View.OnClickListener viewOrderClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skipOrderDetailUI(v, content);
            }
        };
        view.setVisibility(content.isShow() ? View.VISIBLE : View.GONE);
        holder.tvSendOrderInfo.setOnClickListener(content.isShow() ? new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sentOrderInfoMessage(content, message);
                content.setShow(false);
                view.setVisibility(content.isShow() ? View.VISIBLE : View.GONE);
                holder.tvSendOrderInfo.setText(content.isShow() ? R.string.issue_order : R.string.view_details);
                holder.tvSendOrderInfo.setOnClickListener(viewOrderClickListener);
            }
        } : viewOrderClickListener);
        holder.tvSendOrderInfo.setText(content.isShow() ? R.string.issue_order : R.string.view_details);
    }

    public void sentOrderInfoMessage(OrderInfoFloatingMessage content, UIMessage message) {
        OrderInfoMessage orderInfoMessage = new OrderInfoMessage(content.getOrderBean());
        Message myMessage = Message.obtain(message.getTargetId(), Conversation.ConversationType.PRIVATE, orderInfoMessage);
        RongIM.getInstance().sendMessage(myMessage, "订单", "测试", new IRongCallback.ISendMessageCallback() {
            @Override
            public void onAttached(Message message) {
                //消息本地数据库存储成功的回调
                LogUtils.e("zyzd", "customizeMessage:onAttached" + message.toString());
            }

            @Override
            public void onSuccess(Message message) {
                //消息通过网络发送成功的回调
                LogUtils.e("zyzd", "customizeMessage:onSuccess" + message.toString());
            }

            @Override
            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                //消息发送失败的回调
                LogUtils.e("zyzd", "customizeMessage:onError" + message.toString());
            }
        });
        content.setShow(false);
    }

    private void skipOrderDetailUI(View view, OrderInfoFloatingMessage orderInfoFloatingMessage) {
        if (orderInfoFloatingMessage == null || orderInfoFloatingMessage.getOrderBean() == null) return;
        AllOrderBean.DataBean bean = orderInfoFloatingMessage.getOrderBean();

        Bundle bundle = new Bundle();
        bundle.putSerializable(AllOrderFragment.ORDERINFO, bean);
        bundle.putBoolean(OrderFinishedActivity.ISREFRESH_ORDER_LIST_DATA, true);
        bundle.putBoolean(OrderFinishedActivity.ISNORMALORDER, true);
        Intent intent = new Intent();
        intent.putExtras(bundle);

        Context context = view.getContext();
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        if (bean.status == SaleOrderDetailActivity.SALE_FINISHED) {
            intent.setClass(context, OrderFinishedActivity.class);
        } else if (!bean.exType.equals("0")) {//0.卖出,1.买入
            intent.setClass(context, SaleOrderDetailActivity.class);
        } else {
            intent.setClass(context, BuyOrderDetailActivity.class);
        }
        context.startActivity(intent);
    }

    @Override
    public Spannable getContentSummary(OrderInfoFloatingMessage orderFloatMessage) {
        return new SpannableString("订单信息");
    }

    @Override
    public void onItemClick(View view, int i, OrderInfoFloatingMessage orderFloatMessage, UIMessage uiMessage) {
    }

    @Override
    public View newView(Context context, ViewGroup viewGroup) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_chat_order_info_floating, viewGroup, false);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = DataUtils.getScreenWidth(context);
        view.setLayoutParams(layoutParams);
        ViewHolder holder = new ViewHolder();
        holder.tvTransactionAmount = view.findViewById(R.id.tv_transaction_amount);
        holder.tvOrderId = view.findViewById(R.id.tv_order_id);
        holder.tvNums = view.findViewById(R.id.tv_nums);
        holder.tvSendOrderInfo = view.findViewById(R.id.tv_send_order_info);
        view.setTag(holder);
        return view;
    }

    static class ViewHolder {
        TextView tvTransactionAmount;
        TextView tvOrderId;
        TextView tvNums;
        TextView tvSendOrderInfo;
    }
}
